package com.start.security;

import com.start.common.details.BaseUserDetails;
import com.start.common.entity.StartBaseMenu;
import com.start.common.entity.StartBaseRole;
import com.start.common.tools.SpringContextTools;
import com.start.common.vo.SysMenuDataRulesVo;
import com.start.security.handler.IStartPermissionLoad;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class StartUserPermissionTools {
    public static StartUserPermissionTools getInstance() {
        return SpringContextTools.getBean(StartUserPermissionTools.class);
    }


    private static Map<String, IStartPermissionLoad> handlerMap;
    private static boolean isLoad;

    public IStartPermissionLoad getHandlerByUserType(BaseUserDetails userDetails) {
        if (!isLoad) {
            handlerMap = new HashMap<>();
            final Map<String, IStartPermissionLoad> beans = SpringContextTools.getBeans(IStartPermissionLoad.class, false);
            if (beans != null) {
                final Set<Map.Entry<String, IStartPermissionLoad>> entries = beans.entrySet();
                for (Map.Entry<String, IStartPermissionLoad> entry : entries) {
                    if (StringUtils.isBlank(entry.getValue().getUserType())) {
                        continue;
                    }
                    handlerMap.put(entry.getValue().getUserType(), entry.getValue());
                }
            }
            isLoad = true;
        }
        return handlerMap.getOrDefault(userDetails.getStartUserType(), null);
    }

    /**
     * 获取用户菜单
     *
     * @param userDetails
     * @return
     */
    public <T extends StartBaseMenu> List<T> getUserMenu(BaseUserDetails userDetails) {
        final IStartPermissionLoad handlerByUserType = getHandlerByUserType(userDetails);
        if (handlerByUserType == null) return new ArrayList<>();
        return handlerByUserType.getUserMenus(userDetails );
    }

    /**
     * 获取用户角色
     *
     * @param userDetails
     * @return
     */
    public <T extends StartBaseRole> List<T> getUserRole(BaseUserDetails userDetails) {
        final IStartPermissionLoad handlerByUserType = getHandlerByUserType(userDetails);
        if (handlerByUserType == null) return new ArrayList<>();
        return handlerByUserType.getUsrRoles(userDetails);
    }



    /**
     * 获取菜单数据规则
     *
     * @param userDetails 用户信息
     * @param component   组件名称
     * @return
     */
    public List<SysMenuDataRulesVo> getUserMenuDataRulesByComponent(BaseUserDetails userDetails, String component) {
        final IStartPermissionLoad handlerByUserType = getHandlerByUserType(userDetails);
        if (handlerByUserType == null) return new ArrayList<>();
        return handlerByUserType.getUserMenuDataRulesByComponent(userDetails, component);
    }

    public List<SysMenuDataRulesVo> getUserMenuDataRulesByDynamicFormId(BaseUserDetails userDetails, String dynamicFormId) {
        final IStartPermissionLoad handlerByUserType = getHandlerByUserType(userDetails);
        if (handlerByUserType == null) return new ArrayList<>();

        return handlerByUserType.getUserMenuDataRulesByDynamicFormId(userDetails, dynamicFormId);
    }

    /**
     * 判断用户是否拥有角色
     *
     * @param userDetails 用户信息
     * @param roleCodes   角色编码
     * @return
     */
    public boolean hasRole(BaseUserDetails userDetails, String... roleCodes) {
        final IStartPermissionLoad handlerByUserType = getHandlerByUserType(userDetails);
        if (handlerByUserType == null) return false;
        return handlerByUserType.hasRoles(userDetails, roleCodes);
    }

    /**
     * 判断用户是否拥有权限
     *
     * @param userDetails 用户信息
     * @param permissions 权限编码
     * @return
     */
    public boolean hasPermission(BaseUserDetails userDetails, String... permissions) {
        final IStartPermissionLoad handlerByUserType = getHandlerByUserType(userDetails);
        if (handlerByUserType == null) return false;
        return handlerByUserType.hasPermission(userDetails, permissions);
    }


}
